//
//  CLAlgorithmTest.m
//  Charles
//
//  Created by Leo on 2018/6/1.
//  Copyright © 2018年 charles. All rights reserved.
//

#import "CLAlgorithmTest.h"
#import "CLSortFind.h"
#import "CLCharReverse.h"
#import "CLBinaryTree.h"
#import "CLOtherAlgorithm.h"
#import "CLLinkedReverse.h"
#import "CLSortArrayMerge.h"
#import "CLHash.h"
#import "CLLeetCodeAlgorithm.h"

@implementation CLAlgorithmTest

+ (void)testBubbleSort {
    int numbers[10] = {8,3,4,2,10,9,7,1,6,5};
    int length = sizeof(numbers) / sizeof(numbers[0]);
    NSLog(@"%d %d %d %d %d %d %d %d %d %d",
          numbers[0],numbers[1],numbers[2],
          numbers[3],numbers[4],numbers[5],
          numbers[6],numbers[7],numbers[8],
          numbers[9]);
    NSLog(@"--------before sort-------");
    // 调用C函数 bubbleSort
    bubbleSort(numbers,length);
    NSLog(@"--------after sort-------");
    NSLog(@"%d %d %d %d %d %d %d %d %d %d",
          numbers[0],numbers[1],numbers[2],
          numbers[3],numbers[4],numbers[5],
          numbers[6],numbers[7],numbers[8],
          numbers[9]);
}

+ (void)testSelectionSort {
    int numbers[10] = {7,3,4,2,10,9,5,1,6,8};
    int length = sizeof(numbers) / sizeof(numbers[0]);
    NSLog(@"%d %d %d %d %d %d %d %d %d %d",
          numbers[0],numbers[1],numbers[2],
          numbers[3],numbers[4],numbers[5],
          numbers[6],numbers[7],numbers[8],
          numbers[9]);
    NSLog(@"--------before sort-------");
    // 调用C函数 bubbleSort
    selectionSort(numbers, length);
    NSLog(@"--------after sort-------");
    NSLog(@"%d %d %d %d %d %d %d %d %d %d",
          numbers[0],numbers[1],numbers[2],
          numbers[3],numbers[4],numbers[5],
          numbers[6],numbers[7],numbers[8],
          numbers[9]);
}

+ (void)testQuickSort {
    int numbers[10] = {7,3,4,2,10,9,5,1,6,8};
    int length = sizeof(numbers) / sizeof(numbers[0]);
    NSLog(@"%d %d %d %d %d %d %d %d %d %d",
          numbers[0],numbers[1],numbers[2],
          numbers[3],numbers[4],numbers[5],
          numbers[6],numbers[7],numbers[8],
          numbers[9]);
    NSLog(@"--------before sort-------");
    // 调用C函数 quickSort
    quickSort(numbers,0,length - 1);
    NSLog(@"--------after sort-------");
    NSLog(@"%d %d %d %d %d %d %d %d %d %d",
          numbers[0],numbers[1],numbers[2],
          numbers[3],numbers[4],numbers[5],
          numbers[6],numbers[7],numbers[8],
          numbers[9]);
}

+ (void)testBinarySearch {
    int numbers[10] = {7,3,4,2,10,9,5,1,6,8};

    int length = sizeof(numbers) / sizeof(numbers[0]);
    // 将线性表序列化
    selectionSort(numbers, length);
    // 二分查找函数
//    binarySearch(numbers,0,length);
    int goalIndex = binarySearchWithWhile(numbers, length, 2);
    NSLog(@"goalIndex is %d",goalIndex);
}

+ (void)testCharReverse {
    char string[100] = "hello,world";
    /*
     char *string = "hello,world";
     为什么不可以?
     */
    for (unsigned int i = 0; i < strlen(string); i++) {
        printf("%c ",string[i]);
    }
    printf("\n");
    charReverse2(string);
    for (unsigned int i = 0; i < strlen(string); i++) {
        printf("%c ",string[i]);
    }

}



+ (void)testStringToNumber {
    NSString *numberString = @"1234563";
    NSInteger resultNumber = [CLOtherAlgorithm numberStringToInteger:numberString];
    NSLog(@"resultNumber is %ld",resultNumber);
}

+ (void)testLinkedReverse {
    struct Node *head = constructList();
    printList(head);
    
    struct Node *reversedNode = reverseList(head);
    
    printList(reversedNode);
    
}

+ (void)testMergeList {
    // 有序数组合并
    int a[5] = {1,4,6,7,9};
    int b[8] = {2,3,5,6,8,10,11,12};
    // 结果数组
    int result[13]= {};
    // 合并操作
    mergetList(a, 5, b, 8, result);
    printf("merget result is ");
    for (int i = 0; i < 13; i++) {
        printf("%d ",result[i]);
    }
}

+ (void)testHashFind {
    char cha[] = "abaccdeff";
    char fc = findFirstChar(cha);
    printf("this char is %c \n",fc);
}

+ (void)testFindMaxSubString {
    char cha[] = "abcdefcd";
    int length = maxSubStringLength(cha);
    printf("max length is %d \n",length);
}

+ (void)testFindTwonumsum {
    NSArray *resultArray = [CLLeetCodeAlgorithm findTwonumum:@[@2,@7,@11,@15] target:9];
    NSLog(@"resultArray is %@",resultArray);
}

@end
